<?php
// error_reporting(E_ALL);

/**
 * Context Class.
 * Singleton resource for
 * session and site information.
 *
 */

class Context
{
    protected $_user;

	function __construct() 
    {
        global $db ;
        require_once "Db.class.php";
        if( ! isset( $db ) ) {
            $db = new Db() ;
        }

        session_start();

        require_once "User.class.php";
        if($this->IsLoggedIn()) {
            $this->_user = new User($this->SessionUserID());
        }
        else {
            $this->_user = new User();
        }
        $this->LogPage( session_id(),
                        $this->SessionUserID(),
                        "cxContext"
                      );
	}

    function __destruct()
    {
        global $db;
        if( ! isset( $db ) ) {
            $db = new Db() ;
        }

        $this->LogPage( session_id(),
                        $this->SessionUserID(),
                        "dxContext"
                      );
        unset($db);
        session_write_close();
    }

    public function Dump()
    {
        var_dump($this);
    }

    public function User()
    {
        return $this->_user;
    }

    public function UserFullName()
    {
        return $this->User()->FullName();
    }

    private function add_user($email, $password) 
    {
        global $db;
        $sql = "INSERT INTO onebainb_nv.users
                (
                    firstname,
                    lastname,
                    phone,
                    street_address,
                    city_state_zip,
                    business_name,
                    business_address,
                    business_city_state_zip,
                    residence_status,
                    is_business_owner,
                    status
                )
                VALUES
                (
                    '$this->firstname',
                    '$this->lastname',
                    '$this->phone',
                    '$this->street_address',
                    '$this->city_state_zip',
                    '$this->business_name',
                    '$this->business_address',
                    '$this->business_city_state_zip',
                    '$this->residence_status',
                     $this->is_business_owner,
                    '$this->status'
                )";
        $db->SqlExecute($sql);
    }

    public function EmailPasswordToID($eml, $pwd)
    {
        global $db;
        $md5pwd = md5($pwd);
        $sql = "SELECT id FROM onebainb_nv.users
                WHERE email = '{$eml}'
                    AND md5_password = '{$md5pwd}'";
        $id = $db->SqlOneValue($sql);
        return isset($id) ? $id : 0;
    }

    public function LoginForID($id)
    {
        require_once "User.class.php";
        $this->_user = new User($id);
        $this->set_session_user_id($id);
    }

    private function set_session_user_id($id)
    {
        $_SESSION['userid'] = $id;
    }

    private function clear_session_user_id()
    {
        unset($_SESSION['userid']);
    }

    public function SessionUserID()
    {
        return @$_SESSION['userid']; 
    }

    public function IsLoggedIn()
    {
        return isset($_SESSION['userid']);
    }

    public function Logout()
    {
        if($this->IsLoggedIn()) {
            $this->clear_session_user_id();
        }
    }

	public function LogPage( $sessionid = null,
                             $userid = NULL,
                             $eventcode = null,
                             $data = null) 
    {
		global $db;

        require_once "helpers.php";
        $sessionid  = QuoteOrNull($sessionid) ;
        $eventcode  = QuoteOrNull($eventcode) ;
        $data       = QuoteOrNull($data) ;
        $userid     = NumericNull($userid) ;

		$sql = "INSERT INTO onebainb_nv.pagelog
                (
                    sid,
                    uid,
                    eventtime,
                    eventcode,
                    data
                ) VALUES (
                    $sessionid,
                    $userid,
                    NOW(),
                    $eventcode,
                    $data
                )";
		$db->SqlExecute($sql);
	}
}
?>
